package com.smart.ZC.Tools;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;

public class Graphics {
	public Canvas canvas;
	public Paint pen;

	public final static int TRANS_NONE = 0;
	public final static int TRANS_ROT90 = 3;
	public final static int TRANS_ROT180 = 2;
	public final static int TRANS_ROT270 = 1;
	public final static int TRANS_MIRROR = 4;
	public final static int TRANS_MIRROR_ROT90 = 32;
	public final static int TRANS_MIRROR_ROT180 = 5;
	public final static int TRANS_MIRROR_ROT270 = 128;

	public static final int HCENTER = 1;
	public static final int VCENTER = 2;
	public static final int LEFT = 4;
	public static final int RIGHT = 8;
	public static final int TOP = 16;
	public static final int BOTTOM = 32;

	public Graphics() {

	}

	public void setGraphics(Canvas canvas, Paint pen) {
		this.canvas = canvas;
		this.pen = pen;
	}

	public void drawRegone(Bitmap bitmap, int x, int y, int sx, int sy, int sw,
			int sh, int anchor, int rotation, int flag) {
		canvas.drawBitmap(bitmap, new Rect(sx, sy, sw + sx, sh + sw), new Rect(
				x, y, x + sw, y + sh), pen);
	}

	public void drawRegion(Bitmap img, int x_src, int y_src, int w, int h,
			int transform, int x, int y, int anchor) {
		canvas.save();
		switch (transform) {

		case TRANS_NONE:
			if ((anchor & HCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & RIGHT) != 0) {
				x -= w;
			}

			if ((anchor & VCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & BOTTOM) != 0) {
				y -= h;
			}
			break;

		case TRANS_ROT90:
			canvas.rotate(90, x, y);

			if ((anchor & HCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & LEFT) != 0) {
				y -= h;
			}

			if ((anchor & VCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & BOTTOM) != 0) {
				x -= w;
			}

			break;

		case TRANS_ROT180:
			canvas.rotate(180, x, y);

			if ((anchor & HCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & LEFT) != 0) {
				x -= w;
			}

			if ((anchor & VCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & TOP) != 0) {
				y -= h;
			}

			break;

		case TRANS_ROT270:
			canvas.rotate(270, x, y);

			if ((anchor & HCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & RIGHT) != 0) {
				y -= h;
			}

			if ((anchor & VCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & TOP) != 0) {
				x -= w;
			}

			break;

		case TRANS_MIRROR:
			canvas.scale(-1, 1, x, y);

			if ((anchor & HCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & LEFT) != 0) {
				x -= w;
			}

			if ((anchor & VCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & BOTTOM) != 0) {
				y -= h;
			}

			break;

		case TRANS_MIRROR_ROT90:
			canvas.scale(-1, 1, x, y);
			canvas.rotate(270, x, y);

			if ((anchor & HCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & LEFT) != 0) {
				y -= h;
			}

			if ((anchor & VCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & TOP) != 0) {
				x -= w;
			}

			break;

		case TRANS_MIRROR_ROT180:
			canvas.scale(-1, 1, x, y);
			canvas.rotate(180, x, y);

			if ((anchor & HCENTER) != 0) {
				x -= (w >> 1);
			} else if ((anchor & RIGHT) != 0) {
				x -= w;
			}

			if ((anchor & VCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & TOP) != 0) {
				y -= h;
			}

			break;

		case TRANS_MIRROR_ROT270:
			canvas.scale(-1, 1, x, y);
			canvas.rotate(90, x, y);

			if ((anchor & HCENTER) != 0) {
				y -= (h >> 1);
			} else if ((anchor & RIGHT) != 0) {
				y -= h;
			}

			if ((anchor & VCENTER) != 0) {
				x -= (h >> 1);
			} else if ((anchor & BOTTOM) != 0) {
				x -= h;
			}

			break;
		}
		pen.setAntiAlias(true);

		canvas.clipRect(x, y, x + w, y + h);
		canvas.drawBitmap(img, x - x_src, y - y_src, pen);
		canvas.restore();

	}

	public void DrawText(String str, int left, int top, int anchor) {
		pen.setTextAlign(Align.LEFT);
		if ((anchor & LEFT) != 0) {

		} else if ((anchor & HCENTER) != 0) {
			left -= getFontWidth(str) >> 1;
		} else if ((anchor & RIGHT) != 0) {
			left -= getFontWidth(str) >> 1;
		}
		if ((anchor & TOP) != 0) {

		} else if ((anchor & VCENTER) != 0) {
			top += getFontHeight(pen) >> 1;
		} else if ((anchor & BOTTOM) != 0) {
			top += getFontHeight(pen);
		}
		canvas.drawText(str, left, top, pen);
	}

	public int getColor() {

		return pen.getColor();

	}

	public int getFontHeight(Paint pen) {
		FontMetrics fm = pen.getFontMetrics();
		return (int) Math.ceil(fm.descent - fm.top) + 2;
	}

	public int getFontHeight() {
		return getFontHeight(pen);
	}

	public int getFontWidth(String Text) {
		return (int) pen.measureText(Text);
	}

	public void fillRect(int x, int y, int width, int height) {
		Style pre = pen.getStyle();
		pen.setStyle(Style.FILL);
		canvas.drawRect(x, y, x + width, y + height, pen);
		pen.setStyle(pre);
	}

	public void drawLine(int startX, int startY, int stopX, int stopY) {
		canvas.drawLine(startX, startY, stopX, stopY, pen);
	}

	public void drawRect(int left, int top, int right, int bottom) {
		pen.setStyle(Style.STROKE);
		canvas.drawRect(left, top, right, bottom, pen);
	}

	public void DrawImage(Bitmap bitmap, int left, int top, int anchor) {
		pen.setTextAlign(Align.LEFT);
		if ((anchor & LEFT) != 0) {

		} else if ((anchor & HCENTER) != 0) {
			left -= bitmap.getWidth() >> 1;
		} else if ((anchor & RIGHT) != 0) {
			left -= bitmap.getWidth();
		}
		if ((anchor & TOP) != 0) {

		} else if ((anchor & VCENTER) != 0) {
			top -= bitmap.getHeight() >> 1;
		} else if ((anchor & BOTTOM) != 0) {
			top -= bitmap.getHeight();
		}
		canvas.drawBitmap(bitmap, left, top, pen);
	}
}
